/* 此文件用于将数据导出为Execl*/
import {
	returnData
} from '@/utils/dataType'
import Vue from 'vue'
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
import XLSXS from 'yxg-xlsx-style'
import {
	jsonArray
} from '@/api/productApi/file'

// 基础数据下载模板
export function fundBaseData(_this, dataList, headerList) {
	const objectType = [{
		key: 'baseInfo',
		value: '基础信息'
	},
	{
		key: 'consignerInfo',
		value: '委托人信息'
	},
	{
		key: 'dateInfo',
		value: '重要日期信息'
	},
	{
		key: 'investInfo',
		value: '投资相关信息'
	},
	{
		key: 'organInfo',
		value: '机构信息'
	},
	{
		key: 'riskInfo',
		value: '风控信息'
	},
	{
		key: 'rateInfo',
		value: '费率信息'
	}
	]

	const data = jsonArray(dataList)
	const header = jsonArray(headerList)
	data.forEach(item => { // 去掉数组和、__typename字段、字符串字段
		const keys = Object.keys(item)
		keys.forEach(items => {
			if (items === '__typename' || !item[items] || returnData(item[items]) === 'Array' || returnData(item[items]) === 'String') {
				Vue.delete(item, items)
			}
			if (returnData(item[items]) === 'Object') {
				if ('__typename' in item[items]) {
					Vue.delete(item[items], '__typename')
				}
				if ('feeRateInfoList' in item[items]) { // 去掉费率列表
					Vue.delete(item[items], 'feeRateInfoList')
				}
				if ('branchShareList' in item[items]) { // 去掉下属基金列表
					Vue.delete(item[items], 'branchShareList')
				}
			}
		})
	})
	data.forEach(item => { // 去掉空对象
		if (returnData(item) === 'Object') {
			const itemKeys = Object.keys(item)
			itemKeys.forEach(items => {
				if (!Object.keys(item[items]).length) {
					Vue.delete(item, items)
				}
			})
		}
	})

	const msg = `
        <table cellspacing="0" cellpadding="0" border="1" id='fundDatalist'>
            <thead>
                ${headerTemplate(objectType, data, header)}
            </thead>


            </tbody>
                ${listTemplate(jsonArray(data))}
            </tbody>
        </table>
        `
	_this.fundDatalist = msg
}
export function maxProperty(data) { // 取得最多对象属性的那组数据
	const list = []
	data.forEach((item, index) => {
		const keys = Object.keys(item).length
		const obj = {
			key: keys,
			value: index
		}
		if (list.some(items => items.key > obj.key)) {
			list.push(obj)
		} else {
			list.unshift(obj)
		}
	})
	return data[list[0].value]
}

function headerTemplate(objectType, data, header) { // 表头
	let msg = ''
	const fundObj = maxProperty(jsonArray(data))
	// 第一层表头
	msg += '<tr>'
	for (const i in fundObj) {
		const element = fundObj[i]
		const keys = Object.keys(element)
		msg += `<th colspan="${keys.length}" rowspan="1">${objectType.find(obj => obj.key === i).value}</th>`
	}
	msg += '</tr>'
	// 第二层表头
	msg += '<tr>'
	msg += `<th colspan="1" rowspan="1" style="${setAutoWidth(data, 'baseInfo', 'fundCode', '基金代码')}">基金代码</th>`
	msg += `<th colspan="1" rowspan="1" style="${setAutoWidth(data, 'baseInfo', 'fundFullName', '基金名称')}">基金名称</th>`
	for (const i in fundObj) {
		const element = fundObj[i]
		const keys = Object.keys(element)
		keys.forEach(item => {
			if (!['fundCode', 'fundFullName'].includes(item)) {
				msg += `<th colspan="1" rowspan="1" style="${setAutoWidth(data, i, item, header.find(obj => obj.value === item).label)}">${header.find(obj => obj.value === item).label}</th>`
			}
		})
	}
	msg += '</tr>'
	return msg
}

function listTemplate(data) { // 列表
	const fundObj = maxProperty(jsonArray(data))
	const keys = Object.keys(fundObj)
	data.forEach(item => { // 数据不同或者不全的，补全数据
		keys.forEach(items => {
			if (!item[items]) {
				item[items] = jsonArray(fundObj[items])
				const itemsKeys = Object.keys(item[items])
				itemsKeys.forEach(key => { // 补全将属性值去掉
					item[items][key] = ''
				})
			}
		})
	})
	const listItem = [] // 对数据重新排序,避免出现排版错误
	data.forEach(item => {
		const obj = {}
		keys.forEach(items => {
			obj[items] = jsonArray(item[items])
		})
		listItem.push(jsonArray(obj))
	})

	let msg = ''
	listItem.forEach((item, index) => {
		msg += `<tr>`
		msg += `<td>${judgmentTitle(item.baseInfo.fundCode)}</td>`
		msg += `<td>${judgmentTitle(item.baseInfo.fundFullName)}</td>`
		const keys = Object.keys(item)
		keys.forEach((items, indexs) => {
			const childKeys = Object.keys(item[items])
			childKeys.forEach(key => {
				if (!['fundCode', 'fundFullName'].includes(key)) {
					msg += `<td>${judgmentTitle(item[items][key])}</td>`
				}
			})
		})
		msg += `</tr>`
	})

	return msg
}

// 基金经理下载模板
export function fundManageData(_this, dataList, fundRaiseType) {
	const headerList = [{
		value: '基金代码',
		key: 'fundCode',
		type: 'obj'
	},
	{
		value: '基金名称',
		key: 'fundFullName',
		type: 'obj'
	},
	{
		value: '姓名',
		key: 'managerName',
		type: 'arr'
	},
	{
		value: '证件类型',
		key: 'idType',
		type: 'arr'
	},
	{
		value: '证件号码',
		key: 'idNo',
		type: 'arr'
	},
	{
		value: '任职日期',
		key: 'startDate',
		type: 'arr'
	},
	{
		value: '离职日期',
		key: 'endDate',
		type: 'arr'
	}

	]
	if (fundRaiseType === '1') {
		// 导出公募基金，增加 任职公告日期，离职公告日期表头
		headerList.push(
			{
				value: '任职公告日期',
				key: 'entryNoticeDate',
				type: 'arr'
			},
			{
				value: '离职公告日期',
				key: 'leaveNoticeDate',
				type: 'arr'
			}
		)
	}
	const msg = `
    <table cellspacing="0" cellpadding="0" border="1" id='fundMange'>
        <thead>
            <tr>
                ${fundManHeader(headerList, dataList)}
            </tr>
        </thead>

        </tbody>
            ${fundManTemplate(dataList, fundRaiseType)}
        </tbody>
      </table>
    `
	_this.fundMange = msg
}

function fundManTemplate(fundManageList, fundRaiseType) {
	let msg = ''
	fundManageList.forEach(item => {
		if (!item.fundManagerInfoList || !item.fundManagerInfoList.length) {
			msg += `
            <tr>
                <td rowspan="1" colspan="1">${judgmentTitle(item.baseInfo.fundCode)}</td>
                <td rowspan="1" colspan="1">${judgmentTitle(item.baseInfo.fundFullName)}</td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            `
			if (fundRaiseType === '1') {
				// 导出公募基金，增加 任职公告日期，离职公告日期 列
				msg += `<td></td>`
				msg += `<td></td>`
			}
			msg += `</tr>`
		} else {
			item.fundManagerInfoList.forEach((items, indexs) => {
				if (indexs === 0) {
					msg += `
                        <tr>
                            <td rowspan="${item.fundManagerInfoList.length}" colspan="1">${judgmentTitle(item.baseInfo.fundCode)}</td>
                            <td rowspan="${item.fundManagerInfoList.length}" colspan="1">${judgmentTitle(item.baseInfo.fundFullName)}</td>
                            <td rowspan="1" colspan="1">${items.managerName ? items.managerName : ''}</td>
                            <td rowspan="1" colspan="1">${items.idType ? items.idType : ''}</td>
                            <td rowspan="1" colspan="1">${items.idNo ? items.idNo : ''}</td>
                            <td rowspan="1" colspan="1">${items.startDate ? items.startDate : ''}</td>
                            <td rowspan="1" colspan="1">${items.endDate ? items.endDate : ''}</td>
                    `
					if (fundRaiseType === '1') {
						// 导出公募基金，增加 任职公告日期，离职公告日期 列
						msg += `<td rowspan="1" colspan="1">${items.entryNoticeDate ? items.entryNoticeDate : ''}</td>`
						msg += `<td rowspan="1" colspan="1">${items.leaveNoticeDate ? items.leaveNoticeDate : ''}</td>`
					}
					msg += `</tr>`
				} else {
					msg += `
                        <tr>
                            <td rowspan="1" colspan="1">${items.managerName ? items.managerName : ''}</td>
                            <td rowspan="1" colspan="1">${items.idType ? items.idType : ''}</td>
                            <td rowspan="1" colspan="1">${items.idNo ? items.idNo : ''}</td>
                            <td rowspan="1" colspan="1">${items.startDate ? items.startDate : ''}</td>
                            <td rowspan="1" colspan="1">${items.endDate ? items.endDate : ''}</td>
                    `
					if (fundRaiseType === '1') {
						// 导出公募基金，增加 任职公告日期，离职公告日期 列
						msg += `<td rowspan="1" colspan="1">${items.entryNoticeDate ? items.entryNoticeDate : ''}</td>`
						msg += `<td rowspan="1" colspan="1">${items.leaveNoticeDate ? items.leaveNoticeDate : ''}</td>`
					}
					msg += `</tr>`
				}
			})
		}
	})
	return msg
}

function fundManHeader(headerList, dataList) {
	let msg = ''
	const fundMangeList = []
	dataList.forEach(item => {
		if (!!item.fundManagerInfoList && Array.isArray(item.fundManagerInfoList) && item.fundManagerInfoList.length) {
			fundMangeList.push(...item.fundManagerInfoList)
		}
	})
	headerList.forEach(item => {
		if (item.type === 'obj') {
			msg += `<th style="${setAutoWidth(dataList, 'baseInfo', item.key, item.value)}">${item.value}</th>`
		} else {
			msg += `<th style="${setArrayWidth(fundMangeList, item.key, item.value)}">${item.value}</th>`
		}
	})
	return msg
}

// 下属基金下载模板
export function subordinateFunds(_this, dataList) {
	const headerList = [
		{
			value: '基金代码',
			key: 'fundCode',
			type: 'obj'
		},
		{
			value: '基金名称',
			key: 'fundFullName',
			type: 'obj'
		},
		{
			value: '下属基金代码',
			key: 'fundCode',
			type: 'arr'
		},
		{
			value: '下属份额类别简称',
			key: 'fundGradName',
			type: 'arr'
		},
		{
			value: '下属份额类别全称',
			key: 'fundGradFullName',
			type: 'arr'
		},
		{
			value: '下属基金类型',
			key: 'subFundType',
			type: 'arr'
		},
		{
			value: '预计募集金额',
			key: 'estimatedRaisedAmount',
			type: 'arr'
		},
		{
			value: '预计年收益率是否适用',
			key: 'isItSuitable',
			type: 'arr'
		},
		{
			value: '预计年收益率',
			key: 'estimatedAnnualRate',
			type: 'arr'
		},
		{
			value: '份额类别',
			key: 'shareCategory',
			type: 'arr'
		}
	]
	const msg = `
    <table cellspacing="0" cellpadding="0" border="1" id='subFundType'>
        <thead>
            <tr>
                ${subFundHeader(headerList, dataList)}
            </tr>
        </thead>

        </tbody>
            ${subFundTemplate(dataList)}
        </tbody>
      </table>
    `
	_this.subFundType = msg
}

function subFundTemplate(tableData) {
	let msg = ''
	tableData.forEach(item => {
		if (!item.baseInfo.branchShareList || !item.baseInfo.branchShareList.length) {
			msg += `
            <tr>
                <td rowspan="1" colspan="1">${judgmentTitle(item.baseInfo.fundCode)}</td>
                <td rowspan="1" colspan="1">${judgmentTitle(item.baseInfo.fundFullName)}</td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            `
		} else if (Array.isArray(item.baseInfo.branchShareList) && item.baseInfo.branchShareList.length) {
			item.baseInfo.branchShareList.forEach((items, indexs) => {
				if (indexs === 0) {
					msg += `
                        <tr>
                            <td rowspan="${item.baseInfo.branchShareList.length}" colspan="1">${judgmentTitle(item.baseInfo.fundCode)}</td>
                            <td rowspan="${item.baseInfo.branchShareList.length}" colspan="1">${judgmentTitle(item.baseInfo.fundFullName)}</td>
                            <td rowspan="1" colspan="1">${items.fundCode ? items.fundCode : ''}</td>
                            <td rowspan="1" colspan="1">${items.fundGradName ? items.fundGradName : ''}</td>
                            <td rowspan="1" colspan="1">${items.fundGradFullName ? items.fundGradFullName : ''}</td>
                            <td rowspan="1" colspan="1">${items.subFundType ? items.subFundType : ''}</td>
                            <td rowspan="1" colspan="1">${items.estimatedRaisedAmount ? items.estimatedRaisedAmount : ''}</td>
                            <td rowspan="1" colspan="1">${items.estimatedAnnualRate ? items.estimatedAnnualRate : ''}</td>
                            <td rowspan="1" colspan="1">${items.isItSuitable ? items.isItSuitable : ''}</td>
                            <td rowspan="1" colspan="1">${items.shareCategory ? items.shareCategory : ''}</td>
                        </tr>
                    `
				} else {
					msg += `
                        <tr>
                            <td rowspan="1" colspan="1">${items.fundCode ? items.fundCode : ''}</td>
                            <td rowspan="1" colspan="1">${items.fundGradName ? items.fundGradName : ''}</td>
                            <td rowspan="1" colspan="1">${items.fundGradFullName ? items.fundGradFullName : ''}</td>
                            <td rowspan="1" colspan="1">${items.subFundType ? items.subFundType : ''}</td>
							<td rowspan="1" colspan="1">${items.estimatedRaisedAmount ? items.estimatedRaisedAmount : ''}</td>
                            <td rowspan="1" colspan="1">${items.estimatedAnnualRate ? items.estimatedAnnualRate : ''}</td>
                            <td rowspan="1" colspan="1">${items.isItSuitable ? items.isItSuitable : ''}</td>
                            <td rowspan="1" colspan="1">${items.shareCategory ? items.shareCategory : ''}</td>
                        </tr>
                    `
				}
			})
		}
	})
	return msg
}

function subFundHeader(headerList, dataList) {
	let msg = ''
	const branchShareList = []
	dataList.forEach(item => {
		if (item.baseInfo) {
			if (!!item.baseInfo.branchShareList && Array.isArray(item.baseInfo.branchShareList) && item.baseInfo.branchShareList.length) {
				branchShareList.push(...item.baseInfo.branchShareList)
			}
		}
	})
	headerList.forEach(item => {
		if (item.type === 'obj') {
			msg += `<th style="${setAutoWidth(dataList, 'baseInfo', item.key, item.value)}">${item.value}</th>`
		} else {
			msg += `<th style="${setArrayWidth(branchShareList, item.key, item.value)}">${item.value}</th>`
		}
	})
	return msg
}

// 费率下载模板
export function rateInfoExcel(_this, dataList, enumData) {
	const headerList = [{
		value: '基金代码',
		key: 'fundCode',
		type: 'obj'
	},
	{
		value: '基金名称',
		key: 'fundFullName',
		type: 'obj'
	},
	{
		value: '费率类型',
		key: 'feeRateType',
		type: 'arr'
	},
	{
		value: '费率值',
		key: 'feeRate',
		type: 'arr'
	},
	{
		value: '费率算法',
		key: 'feeRateAlgorithm',
		type: 'arr'
	},
	{
		value: '费率备注类型',
		key: 'feeRateTypeRemak',
		type: 'arr'
	},
	{
		value: '费率备注',
		key: 'remark',
		type: 'arr'
	}
	]
	const msg = `
    <table cellspacing="0" cellpadding="0" border="1" id='rateExcel'>
        <thead>
            <tr>
                ${rateHeader(headerList, dataList, enumData)}
            </tr>
        </thead>

        </tbody>
            ${rateTemplate(dataList, enumData)}
        </tbody>
      </table>
    `
	_this.rateExcel = msg
}

function rateTemplate(tableData, enumData) {
	tableData.forEach(item => {
		if (item.rateInfo) {
			if (!!item.rateInfo.feeRateInfoList && Array.isArray(item.rateInfo.feeRateInfoList) && item.rateInfo.feeRateInfoList.length) {
				item.rateInfo.feeRateInfoList = item.rateInfo.feeRateInfoList.filter(items => ['7', '8', '9'].includes(items.feeRateType))
			}
		}
	})
	let msg = ''
	tableData.forEach(item => {
		if (!item.rateInfo || !item.rateInfo.feeRateInfoList || !item.rateInfo.feeRateInfoList.length) {
			msg += `
            <tr>
                <td rowspan="1" colspan="1">${judgmentTitle(item.baseInfo.fundCode)}</td>
                <td rowspan="1" colspan="1">${judgmentTitle(item.baseInfo.fundFullName)}</td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            `
		} else if (Array.isArray(item.rateInfo.feeRateInfoList) && item.rateInfo.feeRateInfoList.length) {
			item.rateInfo.feeRateInfoList.forEach((items, indexs) => {
				if (indexs === 0) {
					msg += `
                        <tr>
                            <td rowspan="${item.rateInfo.feeRateInfoList.length}" colspan="1">${judgmentTitle(item.baseInfo.fundCode)}</td>
                            <td rowspan="${item.rateInfo.feeRateInfoList.length}" colspan="1">${judgmentTitle(item.baseInfo.fundFullName)}</td>
                            <td rowspan="1" colspan="1">${rateTypeTile(items, enumData, '')}</td>
                            <td rowspan="1" colspan="1">${items.feeRate ? items.feeRate : ''}${items.feeRate ? items.feeRateUnit === '2' ? '元/笔' : '%' : ''}</td>
                            <td rowspan="1" colspan="1">${algorithmType(items)}</td>
                            <td rowspan="1" colspan="1">${rateTypeTile(items, enumData, '备注')}</td>
                            <td rowspan="1" colspan="1">${items.remark ? items.remark : ''}</td>
                        </tr>
                    `
				} else {
					msg += `
                        <tr>
                            <td rowspan="1" colspan="1">${rateTypeTile(items, enumData, '')}</td>
                            <td rowspan="1" colspan="1">${items.feeRate ? items.feeRate : ''}${items.feeRate ? items.feeRateUnit === '2' ? '元/笔' : '%' : ''}</td>
							<td rowspan="1" colspan="1">${algorithmType(items)}</td>
                            <td rowspan="1" colspan="1">${rateTypeTile(items, enumData, '备注')}</td>
                            <td rowspan="1" colspan="1">${items.remark ? items.remark : ''}</td>
                        </tr>
                    `
				}
			})
		}
	})
	return msg
}

function rateHeader(headerList, dataList, enumData) {
	let msg = ''
	const tableData = jsonArray(dataList)
	const feeRateInfoList = []
	tableData.forEach(item => {
		if (item.rateInfo) {
			if (!!item.rateInfo.feeRateInfoList && Array.isArray(item.rateInfo.feeRateInfoList) && item.rateInfo.feeRateInfoList.length) {
				item.rateInfo.feeRateInfoList = item.rateInfo.feeRateInfoList.filter(items => ['7', '8', '9'].includes(items.feeRateType))
				feeRateInfoList.push(...item.rateInfo.feeRateInfoList)
			}
		}
	})
	headerList.forEach(item => {
		if (item.type === 'obj') {
			msg += `<th style="${setAutoWidth(dataList, 'baseInfo', item.key, item.value)}">${item.value}</th>`
		} else {
			msg += `<th style="${setRateExcel(feeRateInfoList, item.key, item.value, enumData)}">${item.value}</th>`
		}
	})
	return msg
}

function rateTypeTile(obj, enumData, msg) {
	let title = ''
	if (obj.feeRateType === '9') {
		title = '管理费率'
	} else if (obj.feeRateType === '7') {
		title = '托管费率'
	} else if (obj.feeRateType === '8') {
		if (obj.subFundType) {
			title += `${enumData.subFundType.find(item => item.typeCode === obj.subFundType).typeName}-`
		}
		if (obj.fundCode) {
			title += `${obj.fundCode}-`
		}
		title += '销售服务费率'
	}
	return `${title}${msg}`
}
function algorithmType(item) { // 算法类型
	let msg = ''
	if (item.feeRateType === '9') {
		msg = item.feeRateAlgorithm
	}
	if (item.feeRateType === '7') {
		msg = item.custodyFeeAlgorithm
	}
	if (item.feeRateType === '8') {
		msg = item.salesServiceFeeAlgorithm
	}
	return msg || ''
}

/* 公共方法*/
function judgmentTitle(title) { // 判断是否为空
	return title || ''
}

function setAutoWidth(list, objName, property, title) { // 循环对象类型-数据设置表格自适应width ,title表头标题
	let wpx = null
	if (!title) {
		wpx = 70
	} else if (title.toString().charCodeAt(0) > 255) {
		wpx = title.toString().length * 2 * 7
	} else {
		wpx = title.toString().length * 7
	}
	list.forEach(item => {
		let num = null
		if (!!item[objName] && returnData(item[objName]) === 'Object') {
			const value = item[objName][property]
			if (!value) {
				num = 70
			} else if (value.toString().charCodeAt(0) > 255) {
				num = value.toString().length * 2 * 7
			} else {
				num = value.toString().length * 7
			}
		} else {
			num = 70
		}
		if (wpx) {
			wpx = num > wpx ? num : wpx
		} else {
			wpx = num
		}
	})
	return wpx
}

function setArrayWidth(list, property, title) { // 循环数组类型-数据设置表格自适应width ,title表头标题
	let wpx = null
	if (!title) {
		wpx = 70
	} else if (title.toString().charCodeAt(0) > 255) {
		wpx = title.toString().length * 2 * 7
	} else {
		wpx = title.toString().length * 7
	}
	if (!!list && Array.isArray(list) && list.length) {
		list.forEach(item => {
			let num = null
			if (item[property]) {
				const value = item[property]
				if (!value) {
					num = 70
				} else if (value.toString().charCodeAt(0) > 255) {
					num = value.toString().length * 2 * 7
				} else {
					num = value.toString().length * 7
				}
			} else {
				num = 70
			}
			if (wpx) {
				wpx = num > wpx ? num : wpx
			} else {
				wpx = num
			}
		})
	}
	return wpx
}

function setRateExcel(list, property, title, enumData) { // 循环费率数组-数据设置表格自适应width ,title表头标题
	let wpx = null
	if (!title) {
		wpx = 70
	} else if (title.toString().charCodeAt(0) > 255) {
		wpx = title.toString().length * 2 * 7
	} else {
		wpx = title.toString().length * 7
	}
	if (!!list && Array.isArray(list) && list.length) {
		list.forEach(item => {
			let num = null
			let value = ''
			if (['feeRateType', 'feeRateTypeRemak'].includes(property)) {
				value = rateTypeTile(item, enumData, property === 'feeRateTypeRemak' ? '备注' : '')
			}
			if (!!item[property] && !['feeRateType', 'feeRateTypeRemak'].includes(property)) {
				value = item[property]
			}
			if (property === 'feeRateAlgorithm') {
				if (item.feeRateType === '9') {
					value = item.feeRateAlgorithm
				}
				if (item.feeRateType === '7') {
					value = item.custodyFeeAlgorithm
				}
				if (item.feeRateType === '8') {
					value = item.salesServiceFeeAlgorithm
				}
			}
			if (!value) {
				num = 70
			} else if ([...value.toString()].some(str => str.charCodeAt(0) > 255)) {
				num = value.toString().length * 2 * 7
			} else {
				num = value.toString().length * 7
			}
			if (wpx) {
				wpx = num > wpx ? num : wpx
			} else {
				wpx = num
			}
		})
	}
	return wpx
}

export function excelTemplate(type) {
	const xlsxParam = {
		raw: true
	} // 转换成excel时，使用原始的格式
	const excel = {
		SheetNames: [],
		Sheets: {}
	}
	if (document.querySelector('#fundDatalist')) {
		excel.SheetNames.push('基础数据')
		excel.Sheets['基础数据'] = XLSX.utils.table_to_book(document.querySelector('#fundDatalist'), xlsxParam).Sheets.Sheet1
		const widthList = []
		document.querySelector('#fundDatalist').children[0].children[1].children.forEach(item => {
			const obj = {
				wpx: Number(item.getAttribute('style'))
			}
			widthList.push(obj)
		})
		excel.Sheets['基础数据']['!cols'] = widthList
	}
	if (document.querySelector('#fundMange')) {
		const fundMangeType = type === '1' ? '基金经理' : '投资经理'
		excel.SheetNames.push(fundMangeType)
		excel.Sheets[fundMangeType] = XLSX.utils.table_to_book(document.querySelector('#fundMange'), xlsxParam).Sheets.Sheet1
		const widthList = []
		document.querySelector('#fundMange').children[0].children[0].children.forEach(item => {
			const obj = {
				wpx: Number(item.getAttribute('style'))
			}
			widthList.push(obj)
		})
		excel.Sheets[fundMangeType]['!cols'] = widthList
	}
	if (document.querySelector('#subFundType')) {
		excel.SheetNames.push('下属基金')
		excel.Sheets['下属基金'] = XLSX.utils.table_to_book(document.querySelector('#subFundType'), xlsxParam).Sheets.Sheet1
		const widthList = []
		document.querySelector('#subFundType').children[0].children[0].children.forEach(item => {
			const obj = {
				wpx: Number(item.getAttribute('style'))
			}
			widthList.push(obj)
		})
		excel.Sheets['下属基金']['!cols'] = widthList
	}
	if (document.querySelector('#rateExcel')) {
		excel.SheetNames.push('费率信息')
		excel.Sheets['费率信息'] = XLSX.utils.table_to_book(document.querySelector('#rateExcel'), xlsxParam).Sheets.Sheet1
		const widthList = []
		document.querySelector('#rateExcel').children[0].children[0].children.forEach(item => {
			const obj = {
				wpx: Number(item.getAttribute('style'))
			}
			widthList.push(obj)
		})
		excel.Sheets['费率信息']['!cols'] = widthList
	}

	excel.SheetNames.forEach(item => {
		setExlStyle(excel.Sheets[item])
	})
	const wbout = XLSXS.write(excel, {
		bookType: 'xlsx',
		bookSST: true,
		type: 'buffer'
	})
	try {
		FileSaver.saveAs(
			new Blob([wbout], {
				type: 'application/octet-stream;charset=utf-8'
			}),
			'基金列表.xlsx'
		)
	} catch (e) {
		if (typeof console !== 'undefined') console.log(e, wbout)
	}
	return wbout
}

function setExlStyle(data) {
	for (const key in data) {
		if (data[key] instanceof Object) {
			data[key].s = {
				alignment: {
					horizontal: 'left', // 水平居中对齐
					vertical: 'center' // 垂直居中对齐
				},
				font: {
					sz: 11
				},
				bold: true,
				numFmt: 0,
				wpx: 120
			}
		}
	}
	return data
}
